/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.pojo.Employees;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dine
 */

public class Logout extends ActionSupport {
    
    private InputStream inputStream;
    
    @Action(value = "/logout",results={
        @Result(name = "success", type = "stream", params = {"inputName", "inputStream", "contentType", "text/html"})
    })
    @Override
    public String execute() {
        try {
            Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
            Map<Employees, HttpSession> USERS = (HashMap<Employees, HttpSession>) ServletActionContext.getServletContext().getAttribute("USERS");
            for (Employees e : USERS.keySet()) {
                if (e.getEmployeeid() == emps.getEmployeeid()) {
                    USERS.remove(e).invalidate();
                }
            }
            if (ServletActionContext.getRequest().getSession(false) != null) {
                ServletActionContext.getRequest().getSession(false).invalidate();
            }
            
            inputStream = new StringBufferInputStream("<script type=\"text/javascript\">"
                            + "window.location=\"index.jsp\";"
                            + "</script>");
            return SUCCESS;
        } catch (Exception e) {
            return ERROR;
        }
    }

    public InputStream getInputStream() {
        return inputStream;
    }
    
    
}
